package com.aosan.website.admin.controller

import com.aosan.website.admin.entity.SysUserEntity
import com.aosan.website.admin.service.UserService
import com.aosan.website.common.core.utils.ResultData
import com.aosan.website.common.security.annotation.InnerApi
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import org.slf4j.LoggerFactory
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/user")
class UserController(
        val userService: UserService
) {

    private val logger = LoggerFactory.getLogger(this::class.java)

    @InnerApi
    @GetMapping("/info/{username}")
    fun getUserInfoByUsername(@PathVariable username: String): SysUserEntity? {
        return userService.getUserByUserName(username)
    }

    @GetMapping("/list")
    fun getUserList(page: Page<SysUserEntity>): Any {
        return ResultData.get(userService.getUserList())
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/current")
    fun getCurrentUserInfo(): Any {
        val current = SecurityContextHolder.getContext().authentication
        logger.info(current.toString())
        return ResultData.get(current)
    }
}
